﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using TravelAgencyDAL;
using System.Collections.ObjectModel;

namespace TravelAgencyWpfPictureAdder
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private List<Photo> photos;
        public List<Photo> Photos { get { return this.photos; } }

        public MainWindow()
        {
            InitializeComponent();
            this.photos = new List<Photo>();
            this.RefreshPhotos();
        }

        private void buttonLoadPicture_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            bool? result = dialog.ShowDialog();

            if (result.HasValue && result.Value)
            {
                textBlockImageFileName.Text = dialog.FileName; 
                this.ShowImageInImageToAdd(textBlockImageFileName.Text);
            }            
        }
        
        private void buttonAddImage_Click(object sender, RoutedEventArgs e)
        {
            byte[] allBytes;

            try
            {
                allBytes = File.ReadAllBytes(textBlockImageFileName.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed to read file.");
                return;
            }

            TravelAgencyDAO dao = new TravelAgencyDAO();
            Photo newPhoto = dao.AddPhoto(textBoxImageNameInDatabase.Text, allBytes);
            dao.SaveChanges();

            this.RefreshPhotos();
            this.ShowImageInImageFromDatabase(newPhoto.PhotoId);
        }

        private void RefreshPhotos()
        {
            TravelAgencyDAO dao = new TravelAgencyDAO();
            this.photos = dao.GetPhotos().OrderByDescending(ph => ph.PhotoId).ToList();

            dataGridPhotos.DataContext = null;
            dataGridPhotos.DataContext = this;
        }

        private void dataGridPhotos_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid grid = (DataGrid)sender;

            if (grid.SelectedCells.Count != 0)
            {
                var cellInfo = grid.SelectedCells[0];
                Photo photo = cellInfo.Item as Photo;

                if (photo != null)
                {
                    this.ShowImageInImageFromDatabase(photo.PhotoId);
                }
            }
        }

        private void ShowImageInImageToAdd(string imageFileName)
        {
            byte[] allBytes;

            try
            {
                allBytes = File.ReadAllBytes(imageFileName);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Failed To Read File.");
                return;
            }

            this.imageToAdd.Source = ConvertToImageSource(allBytes);
        }

        private void ShowImageInImageFromDatabase(int photoId)
        {
            TravelAgencyDAO dao = new TravelAgencyDAO();
            Photo photo;

            try
            {
                photo = dao.GetPhotoByPhotoId(photoId);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Photo with this photoId was not found in the database.");
                return;
            }

            this.imageFromDataGrid.Source = ConvertToImageSource(photo.PhotoContent);
        }

        private ImageSource ConvertToImageSource(byte[] allBytes)
        {
            MemoryStream photoContentStream = new MemoryStream(allBytes);

            BitmapImage bitmapImage = new BitmapImage();

            try
            {
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = photoContentStream;
                bitmapImage.EndInit();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to load photo for viewing.");
                return null;
            }

            return bitmapImage;
        }


    }
}
